package cn.com.pc.penguin.servlet;

import cn.com.pc.penguin.core.PenguinEngine;
import cn.com.pc.penguin.core.PenguinArgs;
import cn.com.pc.penguin.service.PenguinSearchResults;
import org.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SearchServlet extends HttpServlet {
	private static final long serialVersionUID = 7375956631058201058L;
	
	public SearchServlet() {
		super();
	}

	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String args = request.getParameter("args");				
		try {
			JSONObject obj = new JSONObject(args);
			PenguinArgs penguinArgs = new PenguinArgs(obj);
			PenguinSearchResults sr = PenguinEngine.searchHandler.search(penguinArgs);

			response.setContentType("text/html;charset=UTF-8");
			response.getWriter().write(sr.toJSON());
			response.getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();			
		}		
	}

	public void init() throws ServletException {
	}
}
